package adv.i31.RobotMin3d;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

/**
 * Contains the handler code for all the buttons on the teachbox
 * @author Max
 *
 */
public class Teachbox
{
	private IRobotController	_controller;

	private Button				buttonYMinus;
	private Button				buttonZMinus;
	private Button				buttonZPlus;
	private Button				buttonYPlus;
	private Button				buttonAus;
	private Button				buttonXPlus;
	private Button				buttonXMinus;
	
	private Button buttonTest1;
	private Button buttonTest2;
	
	private MainActivity parent;

	/**
	 * initializes the Teachbox object. Call this to assign the proper
	 * functions to the buttons on the UI.
	 * @param activity
	 * The main activity
	 * @param controller
	 * The Robotcontroller this teachbox is to control
	 */
	public void init(MainActivity activity, IRobotController controller)
	{
		//
		parent = activity;
		
		_controller = controller;

		buttonYMinus = (Button) parent.findViewById(R.id.buttonYMinusTB);
		buttonZMinus = (Button) parent.findViewById(R.id.buttonZMinusTB);
		buttonZPlus = (Button) parent.findViewById(R.id.buttonZPlusTB);
		buttonYPlus = (Button) parent.findViewById(R.id.ButtonYPlusTB);
		buttonAus = (Button) parent.findViewById(R.id.buttonAusTB);
		buttonXPlus = (Button) parent.findViewById(R.id.buttonXPlusTB);
		buttonXMinus = (Button) parent.findViewById(R.id.buttonXMinusTB);
		
		buttonTest1 = (Button) parent.findViewById(R.id.buttonTest1);
		buttonTest2 = (Button) parent.findViewById(R.id.buttonTest2);
		
		buttonAus.setOnTouchListener(new OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				// Listener did not consume the event
				_controller.sendString("HALLO\r\n");
				return false;
			}
		});

		buttonXMinus.setOnTouchListener(new OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				_controller.Nest();
				// Listener did not consume the event
				return false;
			}
		});

		buttonXPlus.setOnTouchListener(new OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				// Listener did not consume the event
				_controller.Home();
				return false;
			}
		});


		buttonYPlus.setOnTouchListener(new OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				_controller.Draw();
				// Listener did not consume the event
				return false;
			}
		});

		buttonYMinus.setOnTouchListener(new OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				_controller.GripOpen();
				// Listener did not consume the event
				return false;
			}
		});

		buttonZMinus.setOnTouchListener(new OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				
				switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						System.out.println("buttonZMinus ACTION_DOWN");
						return false;
					case MotionEvent.ACTION_UP:
						System.out.println("buttonZMinus ACTION_UP");
						return false;
				}
				// Listener did not consume the event
				return false;
			}
		});

		buttonZPlus.setOnTouchListener(new OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				_controller.DrawStraight();
				// Listener did not consume the event
				return false;
			}
		});
		
		buttonTest1.setOnTouchListener(new OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				_controller.DrawJoint();
				// Listener did not consume the event
				return false;
			}
		});
		
		buttonTest2.setOnTouchListener(new OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				// Listener did not consume the event
				return false;
			}
		});
	}
}
